package demo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;

public class Test4 {
    public static void main(String[] args) throws IOException {
        //1.班级点名,指点一次,点完开始第二次
        BufferedReader br = new BufferedReader(new FileReader("IOTest\\names.txt"));
        //2.将读取到的数据添加到集合中
        //2.1创建集合
        ArrayList<String> list = new ArrayList<>();
        String len;
        while ((len = br.readLine()) != null) {
            list.add(len);
        }
        br.close();
        //3.只创建只有十个元素的集合
        ArrayList<String> stuList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            stuList.add(list.get(i));
        }
        //4.进行随机点名
        //4.1通过HashSet解决重复点名的问题
        randomGetName(list);
        randomGetName(list);



    }

    /**
     * 这里将给到的集合进行随机抽取,每个只点一次,直到点完
     * @param list
     */

    public static void randomGetName(ArrayList<String> list) {
        HashSet<String> hs = new HashSet<>();
        Random r = new Random();
        for (int i = 0; i < list.size(); ){
            int j = r.nextInt(list.size());
            String stu = list.get(j);
            //将抽取到的同学添加到HashSet中
            boolean result = hs.add(stu);
            if (result) {
                i++;
                System.out.print(i + ":" + stu +" ");
            }
        }
        System.out.println();

    }
}
